﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Common;

namespace Business
{
    public partial class CodeDetail
    {
        public static Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT GetPOSMForCombo(ref List<CodeDetail> list)//, bool isAllOptions = false)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = context.CodeDetails.Where(c => c.CMCode == "POSM").OrderBy(o => o.Ordinal);
                list = query.ToList();
                //if (isAllOptions)
                //    list.Insert(0, new ComboItem(0, "Tất cả"));
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SUCCESS;
            }
            catch
            {
                list = null;
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }

        public static Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT GetMechanicismForCombo(ref List<CodeDetail> list)//, bool isAllOptions = false)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = context.CodeDetails.Where(c => c.CMCode == "Mechanicism").OrderBy(o => o.Ordinal);
                list = query.ToList();
                //if (isAllOptions)
                //    list.Insert(0, new ComboItem(0, "Tất cả"));
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SUCCESS;
            }
            catch
            {
                list = null;
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }

        public static Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT GetChannelForCombo(ref List<CodeDetail> list)//, bool isAllOptions = false)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = context.CodeDetails.Where(c => c.CMCode == "Channel").OrderBy(o => o.Ordinal);
                list = query.ToList();
                //if (isAllOptions)
                //    list.Insert(0, new ComboItem(0, "Tất cả"));
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SUCCESS;
            }
            catch
            {
                list = null;
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }

        public static void GetStatusForCombo(ref List<ComboItem> list)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = context.CodeDetails.Where(c => c.CMCode == "RowStatus").OrderBy(o => o.Ordinal);

                foreach (var item in query)
                {
                    ComboItem add = new ComboItem((long)item.NumberValue, item.StandardName);
                    list.Add(add);
                }
                //if (isAllOptions)
                //    list.Insert(0, new ComboItem(0, "Tất cả"));
            }
            catch
            {
                list = null;
                return;
            }
            finally
            {
                context = null;
            }
        }

        public static List<ComboItem> GetForComboByName(string name,bool isAllOptions = false)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = context.CodeDetails.Where(c => c.CMCode == name).OrderBy(o => o.Ordinal);
                List<ComboItem> list = new List<ComboItem>();
                foreach (var item in query)
                {
                    ComboItem add = new ComboItem(item.ID, item.StandardName);
                    list.Add(add);
                }
                if (isAllOptions)
                    list.Insert(0, new ComboItem(0, "Tất cả"));
                return list;
            }
            catch
            {
                return null;
            }
            finally
            {
                context = null;
            }
        }

        public static List<string> GetStringForComboByName(string name)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = context.CodeDetails.Where(c => c.CMCode == name).OrderBy(o => o.NumberValue);
                List<string> list = new List<string>();
                foreach (var item in query)
                {
                    list.Add(item.StandardName);
                }
               
                return list;
            }
            catch
            {
                return null;
            }
            finally
            {
                context = null;
            }
        }
    }
}
